package com.xiaobaibai.bo;

import lombok.Data;
import org.apache.commons.lang3.time.DateUtils;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class BargainDelayItem implements Delayed {

    /**
     * 进入砍价延时队列的item
     * 信息就只有两个
     * 1.bargainUserId
     * 2.过期时间
     */
    private Integer bargainUserId;

    private Date expiredTime;

    public BargainDelayItem(Integer bargainUserId, Date expiredTime) {
        this.bargainUserId = bargainUserId;
        this.expiredTime = expiredTime;
    }

    @Override//获取剩余时间,只有剩余时间为0时,才会被取出
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.getExpiredTime().getTime()-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override//用户延时队列内部排序
    public int compareTo(Delayed o) {
        return this.getExpiredTime().compareTo(((BargainDelayItem)o).getExpiredTime());
    }

    public Integer getBargainUserId() {
        return bargainUserId;
    }

    public void setBargainUserId(Integer bargainUserId) {
        this.bargainUserId = bargainUserId;
    }

    public Date getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(Date expiredTime) {
        this.expiredTime = expiredTime;
    }

}
